﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Accounts;
using LuxDiner.Command.Accounts;
namespace LuxDiner.Logic.Accounts
{
    public class UserLogic
    {
        public static UserModel CreateNewUser(UserModel user)
        {
            user.Password = Utils.Encrypt.MD5Encrypt(user.Password);
            return UserCommand.CreateNewUser(user);
        }

        public static void UpdateUser(UserModel user)
        {
            if (!string.IsNullOrEmpty(user.Password))
                user.Password = Utils.Encrypt.MD5Encrypt(user.Password);
            UserCommand.UpdateUser(user);
        }

        public static void DeleteUser(int id)
        {
            UserCommand.DeleteUser(id);
        }

        public static UserModel GetUser(string guid)
        {
            return UserCommand.GetUser(guid);
        }

        public static UserModel GetUser(int id)
        {
            return UserCommand.GetUser(id);
        }

        public static UserModel GetUserByUserName(string username)
        {
            return UserCommand.GetUserByUserName(username);
        }

        public static UserModel Login(string userName, string password)
        {
            var md5 = Utils.Encrypt.MD5Encrypt(password);
            return UserCommand.Login(userName, md5);
        }

        #region membership

        public static UserCollection FindUsersInRole(string roleName, string usernameToMatch)
        {
            return UserCommand.FindUsersInRole(roleName, usernameToMatch);
        }

        public static UserCollection GetUsersInRole(string roleName)
        {
            return UserCommand.GetUsersInRole(roleName);
        }

        public static bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            var md5Old = Utils.Encrypt.MD5Encrypt(oldPassword);
            var md5New = Utils.Encrypt.MD5Encrypt(newPassword);
            return UserCommand.ChangePassword(userName, md5Old, md5New);
        }

        public static bool IsUserInRole(string username, string roleName)
        {
            return UserCommand.IsUserInRole(username, roleName);
        }

        public static void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            UserCommand.RemoveUsersFromRoles(usernames, roleNames);
        }
        #endregion

        public static UserCollection GetUsers(UserQuery sea)
        {
            return UserCommand.GetUsers(sea);
        }
    }
}
